/**********************************************
* @file    	i2c_ee.h
* @author 	dralee
* @version	v1.0
* @date		2021-12-06
* @brief	I2C访问EEPROM应用
*		AT24C02	2kb=2048bit = 2048/8 B=256B
*		32 pages of 8 bytes each
* Device Address
* 1 0 1 0 A2 A1 A0 R/W
* 1 0 1 0 0  0  0  0 = 0xA0
* 1 0 1 0 0  0  0  1 = 0xA1
*
* AT24C01/02每页有8个字节
* AT24C04/08A/16A每页有16个字节
***********************************************/
#ifndef __I2C_EEPROM_H
#define __I2C_EEPROM_H

#include "stm32f10x.h"
/**************************************************************************/
#define	EEPROM_I2Cx											I2C1
#define	EEPROM_I2C_APBxClock_FUN				RCC_APB1PeriphClockCmd
#define	EEPROM_I2C_CLK									RCC_APB1Periph_I2C1
#define EEPROM_I2C_GPIO_APBxClock_FUN		RCC_APB2PeriphClockCmd
#define EEPROM_I2C_GPIO_CLK							RCC_APB2Periph_GPIOB
#define EEPROM_I2C_SCL_PORT							GPIOB
#define EEPROM_I2C_SCL_PIN							GPIO_Pin_6
#define EEPROM_I2C_SDA_PORT							GPIOB
#define EEPROM_I2C_SDA_PIN							GPIO_Pin_7

#define I2C_Speed												400000		// SMT32 I2C快速模式400KHz
#define I2Cx_OWN_ADDRESS7								0x0A			// 该地址只要与STM32外挂的I2C器件地址不一样即可
#define I2C_PageSize										8					// AT24C01/02每页有8个字节
//#define I2C_PageSize										16					// AT24C04/08A/16A每页有16个字节

// 等待超时
#define I2CT_FLAG_TIMEOUT								((uint32_t)0x1000)
#define I2CT_LONG_TIMEOUT								((uint32_t)(10 * I2CT_FLAG_TIMEOUT))

// 信息输出
#define EEPROM_DEBUG_ON									0
#define EEPROM_INFO(fmt,arg...)					printf("<<-EEPROM-INFO->> "fmt"\n", ##arg)
#define EEPROM_ERROR(fmt,arg...)				printf("<<-EEPROM-ERROR->> "fmt"\n", ##arg)
#define EEPROM_DEBUG(fmt,arg...)				do{\
																					if(EEPROM_DEBUG_ON)\
																						printf("<<-EEPROM-DEBUG->> [%d]"fmt"\n", __LINE__, ##arg);\
																				}while(0)

// EEPROM Addresses defines
#define EEPROM_Block0_ADDRESS					0xA0		// E2=0
//#define EEPROM_Block1_ADDRESS					0xA2		// E2=0
//#define EEPROM_Block2_ADDRESS					0xA4		// E2=0
//#define EEPROM_Block3_ADDRESS					0xA6		// E2=0

void I2C_EEP_Init(void);
void I2C_EEP_BufferWrite(uint8_t *pWriteBuf, uint8_t usAddress, uint16_t usSize);
uint32_t I2C_EEP_ByteWrite(uint8_t *pWriteBuf, uint8_t writeAddress);
uint32_t I2C_EEP_PageWrite(uint8_t *pWriteBuf, uint8_t writeAddress, uint8_t byteNumToWrite);
uint32_t I2C_EEP_BufferRead(uint8_t *pReadBuf, uint8_t readAddress, uint16_t byteNumToRead);
void I2C_EEP_WaitEEPROMStandbyState(void);

#endif
